package com.Zhengxin.smarthome;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class Music extends Activity{
	private TextView musicName;
	private ImageView VolumeIco;
	private ImageButton Before;
	private ImageButton Play;
	private ImageButton Next;
	private ImageButton Run;
	private SeekBar Volume;
	private int click=0;
	private int click2=0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.music);	
		
		//SharkChange();
		BuildUI();
	}

	private void BuildUI() {
		// TODO Auto-generated method stub
		//musicName=(TextView)findViewById(R.id.musicName);
		Before=(ImageButton)findViewById(R.id.Before);
		Run=(ImageButton)findViewById(R.id.Run);
		Play=(ImageButton)findViewById(R.id.Play);
		Next=(ImageButton)findViewById(R.id.Next);
		VolumeIco=(ImageView)findViewById(R.id.VolumeIco);
		Volume=(SeekBar)findViewById(R.id.Volume);
		
		
		Before.setImageResource(R.drawable.before);
		Play.setImageResource(R.drawable.play);
		Next.setImageResource(R.drawable.next);
		VolumeIco.setImageResource(R.drawable.volumemuted);
		Run.setImageResource(R.drawable.start);
		
		Run.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
//				if(click2==0);{				
//					Intent intent= new Intent(Music.this,ConnectService.class);
//					Bundle bundle= new Bundle();
//					bundle.putString("config","Music_CONTROL_Run");
//					intent.putExtras(bundle);
//					startService(intent);
//					click2=1;
//				}
				
				//if(click2==1){					
					new AlertDialog.Builder(Music.this)
					.setMessage("What are you doing?")
					.setPositiveButton("Start",new android.content.DialogInterface.OnClickListener(){

						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							Intent intent= new Intent(Music.this,ConnectService.class);
							Bundle bundle= new Bundle();
							bundle.putString("config","Music_CONTROL_Run");
							intent.putExtras(bundle);
							startService(intent);
						}
						
					}).setNegativeButton("Exit", new DialogInterface.OnClickListener(){

						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							Intent intent= new Intent(Music.this,ConnectService.class);
							Bundle bundle= new Bundle();
							bundle.putString("config","Music_CONTROL_Exit");
							intent.putExtras(bundle);
							startService(intent);
							click2=0;
						}
						
					}).show();
					
				//}
			}
		});
		
		Play.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
//				if(click==0){
//					Play.setImageResource(R.drawable.pause);
//					
//					Intent intent= new Intent(Music.this,ConnectService.class);
//					Bundle bundle= new Bundle();
//					bundle.putString("config","Music_CONTROL_Run");
//					intent.putExtras(bundle);
//					startService(intent);
//					
//					click=1;
//				}
				if(click==0){
					Play.setImageResource(R.drawable.pause);
					
					Intent intent= new Intent(Music.this,ConnectService.class);
					Bundle bundle= new Bundle();
					bundle.putString("config","Music_CONTROL_Start");
					intent.putExtras(bundle);
					startService(intent);
					
					click=1;
				}
				else{
                    Play.setImageResource(R.drawable.play);
					
					Intent intent= new Intent(Music.this,ConnectService.class);
					Bundle bundle= new Bundle();
					bundle.putString("config","Music_CONTROL_Pause");
					intent.putExtras(bundle);
					startService(intent);
					
					click=0;
				}
			}
		});
		
		Before.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent= new Intent(Music.this,ConnectService.class);
				Bundle bundle= new Bundle();
				bundle.putString("config","Music_CONTROL_Before");
				intent.putExtras(bundle);
				startService(intent);
			}
		});
		
		Next.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent= new Intent(Music.this,ConnectService.class);
				Bundle bundle= new Bundle();
				bundle.putString("config","Music_CONTROL_Next");
				intent.putExtras(bundle);
				startService(intent);
			}
		});
		
		Volume.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// TODO Auto-generated method stub
				
				Intent intent= new Intent(Music.this,ConnectService.class);
				Bundle bundle= new Bundle();
				String message="Music_SETVOLUME_"+String.valueOf(progress);
				bundle.putString("config",message);
				intent.putExtras(bundle);
				startService(intent);
				//set
				
				if(progress==1){
					VolumeIco.setImageResource(R.drawable.volumemuted);
				}
				if(progress<=33&&progress>1){
					VolumeIco.setImageResource(R.drawable.volumelow);
				}
				if(progress<=66&&progress>33){
					VolumeIco.setImageResource(R.drawable.volumemedium);
				}
				if(progress<=100&&progress>66){
					VolumeIco.setImageResource(R.drawable.volumehigh);
				}
			}
		});
	}
	

}
